/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package com.ecarx.notificationx;

import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringDef;
import androidx.core.graphics.drawable.IconCompat;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NotificationHelper {

    public static final String EXTRA_SUGGEST_ACTION_LIST = "com.ecarx.suggest_action_list";
    public static final String EXTRA_NOTIFICATION_TYPE = "com.ecarx.notification_type";
    public static final String EXTRA_IMAGE = "com.ecarx.image";
    public static final String EXTRA_NEED_TTS = "com.earx.need_tts";
    public static final String EXTRA_SHOW_SUGGEST_ACTIONS = "com.earx.show_suggest_actions";
    public static final String EXTRA_NOTIFICATION = "com.ecarx.notification";
    public static final String EXTRA_SMALL_TIP = "com.ecarx.small_tip";
    public static final String EXTRA_LARGE_TIP = "com.ecarx.large_tip";
    public static final String EXTRA_NEED_SHOW_BUBBLE = "com.ecarx.need_show_bubble";
    public static final String EXTRA_BUBBLE_TEXT = "com.ecarx.bubble_text";

    @NonNull
    public static ArrayList<Bundle> actionsToBundles(@NonNull List<ActionCompat2> actions) {
        ArrayList<Bundle> bundles = new ArrayList<>();
        for (ActionCompat2 action : actions) {
            bundles.add(action.toBundle());
        }
        return bundles;
    }

    @NonNull
    public static List<ActionCompat2> bundlesToActions(@NonNull List<Bundle> bundles) {
        List<ActionCompat2> actions = new ArrayList<>();
        for (Bundle bundle : bundles) {
            actions.add(ActionCompat2.fromBundle(bundle));
        }
        return actions;
    }

    public interface NotificationType {

        String PLAIN_TEXT = "plain_text";
        String LIGHT_CARD = "light_card";
        String TIP_LIGHT_CARD = "tip_light_card";
        String BUBBLE_TIP_LIGHT_CARD = "bubble_tip_light_card";
        String HEAVY_CARD = "heavy_card";
        String TIP_HEAVY_CARD = "tip_heavy_card";

    }

    public interface CardType {

        String VOICE = "voice";
        String LAUNCHER = "launcher";

    }

    @StringDef({NotificationType.PLAIN_TEXT, NotificationType.LIGHT_CARD, NotificationType.TIP_LIGHT_CARD, NotificationType.HEAVY_CARD, NotificationType.TIP_HEAVY_CARD})
    @Retention(RetentionPolicy.SOURCE)
    public @interface NOTIFICATION_TYPE_A {
    }

    @StringDef({CardType.VOICE, CardType.LAUNCHER})
    @Retention(RetentionPolicy.SOURCE)
    public @interface CARD_TYPE_A {
    }

    @NonNull
    public static List<ActionCompat2> getSuggestActions(@NonNull StatusBarNotification notification) {
        List<Bundle> bundles = notification.getNotification().extras.getParcelableArrayList(EXTRA_SUGGEST_ACTION_LIST);
        if (bundles == null) {
            return Collections.emptyList();
        }

        return bundlesToActions(bundles);
    }

    @Nullable
    public static String getNotificationType(@NonNull StatusBarNotification notification) {
        return notification.getNotification().extras.getString(EXTRA_NOTIFICATION_TYPE);
    }

    @Nullable
    public static IconCompat getImage(@NonNull StatusBarNotification notification) {
        Bundle iconBound = notification.getNotification().extras.getParcelable(EXTRA_IMAGE);
        if (iconBound == null) {
            return null;
        }

        return IconCompat.createFromBundle(iconBound);
    }

    public static boolean getNeedTTS(@NonNull StatusBarNotification notification) {
        return notification.getNotification().extras.getBoolean(EXTRA_NEED_TTS);
    }

    public static boolean getShowSuggestActions(@NonNull StatusBarNotification notification) {
        return notification.getNotification().extras.getBoolean(EXTRA_SHOW_SUGGEST_ACTIONS);
    }

    /**
     * 点击操作提示。
     * <p>
     * e.g.: "一起看看路况吧"
     */
    public static boolean getNeedBubble(@NonNull StatusBarNotification notification) {
        return notification.getNotification().extras.getBoolean(EXTRA_NEED_SHOW_BUBBLE);
    }

    /**
     * 点击操作提示。
     * <p>
     * e.g.: "一起看看路况吧"
     */
    @Nullable
    public static String getBubbleText(@NonNull StatusBarNotification notification) {
        return notification.getNotification().extras.getString(EXTRA_BUBBLE_TEXT);
    }

    @Nullable
    public static ActionCompat2 getSmallTip(@NonNull StatusBarNotification notification) {
        Bundle bundle = notification.getNotification().extras.getBundle(EXTRA_SMALL_TIP);
        if (bundle == null) {
            return null;
        }

        return ActionCompat2.fromBundle(bundle);
    }

    @Nullable
    public static ActionCompat2 getLargeTip(@NonNull StatusBarNotification notification) {
        Bundle bundle = notification.getNotification().extras.getBundle(EXTRA_LARGE_TIP);
        if (bundle == null) {
            return null;
        }

        return ActionCompat2.fromBundle(bundle);
    }

    @Nullable
    public static Notification getNotification(@NonNull Intent intent) {
        return intent.getParcelableExtra(EXTRA_NOTIFICATION);
    }

    public static void setNotification(@NonNull Intent intent, @NonNull Notification notification) {
        intent.putExtra(NotificationHelper.EXTRA_NOTIFICATION, notification);
    }

    @Nullable
    public static String getText(@NonNull StatusBarNotification notification) {
        return notification.getNotification().extras.getString(Notification.EXTRA_TEXT);
    }

}
